package com.party.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.party.business.domain.*;
import com.party.business.service.*;
import com.party.business.service.impl.KcTypeServiceImpl;
import com.party.common.annotation.Log;
import com.party.common.annotation.RepeatSubmit;
import com.party.common.config.RuoYiConfig;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.domain.entity.SysDept;
import com.party.common.core.domain.entity.SysDictData;
import com.party.common.core.domain.entity.SysUser;
import com.party.common.core.domain.model.LoginUser;
import com.party.common.core.domain.model.RegisterBody;
import com.party.common.core.page.TableDataInfo;
import com.party.common.enums.BusinessType;
import com.party.common.exception.ServiceException;
import com.party.common.utils.*;
import com.party.common.utils.poi.ExcelUtil;
import com.party.system.domain.SysConfig;
import com.party.system.service.*;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.rmi.ServerException;
import java.rmi.server.ExportException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 小程序接口
 */
@RestController
@RequestMapping("/webApi")
public class TWebApiController extends BaseController {
//
    @Autowired
    private ITLbImageService tLbImageService;
//
//
//    @Autowired
//    private ITNoticeService tNoticeService;
//
//
//    @Autowired
//    private ITNewsService tNewsService;
//
//
//    @Autowired
//    private ISysUserService sysUserService;
//
//    @Autowired
//    private ITFjFileService tFjFileService;
//
    @Autowired
    private ISysUserService sysUserService;
//
//
    @Autowired
    private IKcTypeService kcTypeService;
//
//
    @Autowired
    private ITTwxxService tTwxxService;
//


    @Autowired
    private IZqTypeService zqTypeService;


    @Autowired
    private IKcService kcService;


    @Autowired
    private ITSearchHisService tSearchHisService;


    /**
     * 保存用户的搜索记录
     */
    @PostMapping("searchHis/add")
    public AjaxResult searchHisAdd(@RequestBody TSearchHis tSearchHis) {
        tSearchHis.setContent(tSearchHis.getContent());
        tSearchHis.setUserId(getUserId()+"");
        return toAjax(tSearchHisService.save(tSearchHis));
    }

    /**
     * 用户的搜索记录
     */

    @GetMapping("/searchHisList" )
    public AjaxResult searchHisList(TSearchHis tSearchHis) {
        // 查询最近三天的
        PageUtils.startPage(1,20);
        tSearchHis.setUserId(SecurityUtils.getUserIdStr());
        List<TSearchHis> list = tSearchHisService.selectTSearchHisList(tSearchHis);
        return AjaxResult.success(list);
    }

//
    /**
     * 注册协议和隐私政策  关于我们
     */
    @GetMapping("/nologin/twxxlist")
    public   AjaxResult twxxlist(TTwxx tTwxx) {
        List<TTwxx> list = tTwxxService.selectTTwxxList(tTwxx);
        return AjaxResult.success(list);
    }

    /**
     * 轮播图列表
     */
    @GetMapping("/nologin/getLbImageList")
    public AjaxResult getLbImageList(TLbImage tLbImage) {
        List<TLbImage> list = tLbImageService.selectTLbImageList(tLbImage);
        return AjaxResult.success(list);
    }

    /**
     * 课程类型列表
     */
    @GetMapping("/nologin/getKcTypeList")
    public AjaxResult getKcTypeList(KcType kcType) {
        List<KcType> kcTypes = kcTypeService.selectKcTypeList(kcType);
        return AjaxResult.success(kcTypes);
    }


    /**
     * 专区类型列表
     */
    @GetMapping("/nologin/getZqTypeList")
    public  AjaxResult  getZqTypeList(ZqType zqType) {
        List<ZqType> zqTypes = zqTypeService.selectZqTypeList(zqType);
        return AjaxResult.success(zqTypes);
    }


    //

    /**
     * 用户个人信息
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getInfo() {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserByIdApp(userId);
        return AjaxResult.success(sysUser);
    }


    /**
     * 课程列表
     */
    @GetMapping("/nologin/kclist" )
    public TableDataInfo kclist(Kc kc) {
        startPage();
        kc.setStatus("1");
        List<Kc> list = kcService.selectKcListApp(kc);
        return getDataTable(list) ;
    }

    /**
     * 课程详情
     */
    @GetMapping(value = "/kcDetail/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        Kc  kc=new  Kc();
        kc.setId(id);
         kc.setUserId(SecurityUtils.getUserIdStr());
        Kc kc1 = kcService.selectKcByIdApp(kc);
        if(kc1==null){
            throw  new ServiceException("课程已删除");
        }
        if("1".equals(kc1.getDelFlag())){
           throw  new ServiceException("课程已删除");
        }
        return  AjaxResult.success(kc1);
    }


    @Autowired
    private ITOrderService tOrderService;


    /**
     * 课程购买记录
     */
    @GetMapping("/nologin/kcOrderFinishList" )
    public TableDataInfo kcOrderFinishList(TOrder tOrder) {
        startPage();
        List<TOrder> list = tOrderService.selectTOrderListApp(tOrder);
        return getDataTable(list);
    }


    @Autowired
    private ITScService tScService;

    /**
     * 我的----------------收藏列表
     */
    @GetMapping("/mysckclist")
    public  TableDataInfo mysckclist( ) {
        startPage();
        Kc  kc=new  Kc();
        kc.setUserId(SecurityUtils.getUserIdStr());
        List<Kc> list = kcService.selectKcListScApp(kc);
        return getDataTable(list);
    }

    /**
     * 取消收藏--传入课程Id
     */
    @PostMapping("/cancleScKc")
    public AjaxResult cancleScKc(@RequestBody  Kc  kc) {
        String kcId = kc.getId();
        if(StringUtils.isEmpty(kcId)){
            return AjaxResult.error("课程id不能为空");
        }
        //根据 businessId 和 userId 删除 TSc
        QueryWrapper<TSc>  queryWrapper=new  QueryWrapper<>();
        queryWrapper.eq("business_id",kcId);
        queryWrapper.eq("user_id",SecurityUtils.getUserIdStr());
        tScService.remove(queryWrapper);
        return   AjaxResult.success();
    }

    /**
     * 收藏--传入课程Id
     */
    @PostMapping("/addKcSc")
    public AjaxResult addKcSc(@RequestBody  Kc  kc) {
        String kcId = kc.getId();
        if(StringUtils.isEmpty(kcId)){
            return AjaxResult.error("课程id不能为空");
        }
        // 先判断是否收藏过
        QueryWrapper<TSc>  queryWrapper=new  QueryWrapper<>();
        queryWrapper.eq("business_id",kcId);
        queryWrapper.eq("user_id",SecurityUtils.getUserIdStr());
        if(tScService.count(queryWrapper)>0){
            return  AjaxResult.error("已收藏");
        }
        //根据 businessId 和 userId 删除 TSc
        TSc tSc=new  TSc();
        tSc.setUserId(SecurityUtils.getUserIdStr());
        tSc.setBusinessId(kcId);
        tScService.save(tSc);
        return   AjaxResult.success();
    }



//
//
    @Autowired
    private ISysConfigService configService;
//
    /**
     * 客服电话   客服微信号
     */
    @GetMapping("/nologin/getxtpzList")
    public   AjaxResult getxtpzList() {
        SysConfig sysConfig = new SysConfig();
        List<SysConfig> sysConfigs = configService.selectConfigList(sysConfig);
        return AjaxResult.success(sysConfigs);
    }



    @Autowired
    private ITCommentService tCommentService;





    /**
     * 我的订单
     */
    @GetMapping("/myOrderList")
    public  TableDataInfo myOrderList() {
        startPage();
        TOrder  order=new TOrder();
        order.setUserId(SecurityUtils.getUserIdStr());
        List<TOrder> tOrders = tOrderService.selectTOrderListApp(order);
        for (TOrder tOrder : tOrders) {
            // 判断是否评价
            QueryWrapper<TComment> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("business_id",tOrder.getId());
            queryWrapper.eq("user_id",SecurityUtils.getUserIdStr());
            long count = tCommentService.count(queryWrapper);
            tOrder.setIsFj(count>0?1:0);
        }
        return   getDataTable(tOrders);
    }


//

//
//

//
//
//    @Autowired
//    private ITKcService tKcService;
//
//
//    /**
//     * 课程列表
//     */
//    @GetMapping("/getKcList")
//    public TableDataInfo getKcList(TKc kc) {
//        startPage();
//        List<TKc> tKcs = tKcService.selectTKcListApp(kc);
//        return getDataTable(tKcs);
//    }
//
//
//    /**
//     * 课程详情
//     */
//    @GetMapping(value = "/getkcDetail/{id}")
//    public AjaxResult getkcDetail(@PathVariable("id") String id) {
//        return AjaxResult.success(tKcService.selectTKcById(id));
//    }
//



    /**
     * 用户个人信息--修改
     */
    @PutMapping("updateUser")
    public AjaxResult updateUser(@RequestBody SysUser user) {
        user.setUpdateBy(getUsername());
        user.setUserId(SecurityUtils.getUserId());
        return toAjax(sysUserService.updateUserApp(user));
    }
//
//    /**
//     * 用户个人信息--用户注销
//     */
//    @DeleteMapping("/removeUser")
//    public AjaxResult removeUser() {
//        String username = SecurityUtils.getUsername();
//        if ("admin".equals(username)) {
//            return AjaxResult.error("超级管理员不能注销");
//        }
//        Long[] userIds = {SecurityUtils.getUserId()};
//        return toAjax(userService.deleteUserAppByIds(userIds));
//    }



}
